<template>
  <a-layout-header :class="[headerTheme, 'admin-header']">
    <div :class="['admin-header-wide', layout, pageWidth]">
      <router-link
        v-if="isMobile || layout === 'head'"
        to="/"
        :class="['logo', isMobile ? null : 'pc', headerTheme]"
      >
        <!-- <img width="32" src="@/assets/img/logo.png" /> -->
        <h1 v-if="!isMobile" style="margin-left: 10px">{{ systemName }}</h1>

        <div v-if="isMobile">
          <h1 style="font-size: 20px">{{ systemName }}</h1>
        </div>
      </router-link>
      <a-icon
        v-if="layout !== 'head' && !isMobile"
        class="trigger"
        :type="collapsed ? 'menu-unfold' : 'menu-fold'"
        @click="toggleCollapse"
      />
      <div class="admin-header-menu" v-if="layout == 'side'">
        <a-breadcrumb>
          <a-breadcrumb-item :key="index" v-for="(item, index) in breadcrumb">
            <span>{{ item }}</span>
          </a-breadcrumb-item>
        </a-breadcrumb>
      </div>
      <div
        v-if="layout !== 'side' && !isMobile"
        class="admin-header-menu"
        :style="`width: ${menuWidth};`"
      >
        <i-menu
          class="head-menu"
          :theme="headerTheme"
          mode="horizontal"
          :options="menuData"
          @select="onSelect"
        />
      </div>
      <div :class="['admin-header-right', headerTheme]">
        <div class="header-item" @click="fullscreen">
          <a-icon :type="fullScreenIcon" />
        </div>

        <header-short-cut
          ref="headershortcut"
          class="header-item"
          @shortCut="shortCutModal"
        />

        <header-notice class="header-item" />

        <header-avatar class="header-item" @changePwd="changePwdModal" />
      </div>
    </div>

    <change-pwd
      ref="changepwd"
      v-if="changepwd.visible"
      :visible.sync="changepwd.visible"
      :title="changepwd.title"
    ></change-pwd>

    <short-cut
      ref="shortcut"
      v-if="shortcut.visible"
      :visible.sync="shortcut.visible"
      :title="shortcut.title"
      @ok="shortCutOk"
    ></short-cut>
  </a-layout-header>
</template>

<script>
import ChangePwd from "@/pages/system/user/changepwd";
import HeaderNotice from "./HeaderNotice";
import ShortCut from "@/pages/system/user/shortcut";
import HeaderAvatar from "./HeaderAvatar";
import HeaderShortCut from "./HeaderShortCut";
import IMenu from "@/components/menu/menu";
import screenfull from "screenfull";
import { mapState, mapMutations } from "vuex";

export default {
  name: "AdminHeader",
  components: {
    IMenu,
    HeaderAvatar,
    HeaderShortCut,
    ChangePwd,
    ShortCut,
    HeaderNotice,
  },
  props: ["collapsed", "menuData", "breadcrumb"],
  data() {
    return {
      changepwd: {
        visible: false,
        title: "",
      },
      shortcut: {
        visible: false,
        title: "",
      },
      isFullscreen: false,
      fullScreenIcon: "fullscreen",
    };
  },
  computed: {
    ...mapState("setting", [
      "theme",
      "isMobile",
      "layout",
      "systemName",
      "lang",
      "pageWidth",
    ]),

    /**
     *
     */
    headerTheme() {
      if (
        this.layout == "side" &&
        this.theme.mode == "dark" &&
        !this.isMobile
      ) {
        return "light";
      }
      return this.theme.mode;
    },

    /**
     * 菜单宽度
     */
    menuWidth() {
      const { layout } = this;
      //得到右侧宽度
      const headWidth =
        layout === "head"
          ? "100% - 200px"
          : layout === "mix"
          ? "100% - 60px"
          : "100%";
      const extraWidth = "320px";
      return `calc(${headWidth} - ${extraWidth})`;
    },
  },
  methods: {
    /**
     * 重置密码
     */
    changePwdModal() {
      this.changepwd.visible = true;
      this.changepwd.title = "修改密码";
    },

    /**
     * 快捷方式
     */
    shortCutModal() {
      this.shortcut.visible = true;
      this.shortcut.title = "快捷方式";
    },

    /**
     * 保存完毕,重新刷新快捷方式
     */
    shortCutOk() {
      this.$refs.headershortcut.init();
    },
    /**
     * 全屏
     */
    fullscreen() {
      if (!screenfull.isEnabled) {
        this.$message({
          message: "您的浏览器不支持全屏",
          type: "warning",
        });
        return false;
      }
      screenfull.toggle();
      this.isFullscreen = !this.isFullscreen;
      this.fullScreenIcon = this.isFullscreen
        ? "fullscreen-exit"
        : "fullscreen";
    },
    toggleCollapse() {
      this.$emit("toggleCollapse");
    },
    onSelect(obj) {
      this.$emit("menuSelect", obj);
    },
    ...mapMutations("setting", ["setLang"]),
  },
};
</script>

<style lang="less" scoped>
@import "index";
</style>
